import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:table_calendar/table_calendar.dart';

typedef PickerCallback = void Function(dynamic value);
typedef EvenLoadCallback = List<bool> Function(DateTime day);


class TableCalendarDialog extends Dialog {
  const TableCalendarDialog({super.key, this.onEvenLoad});

  final EvenLoadCallback? onEvenLoad;

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Container(
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(60.r)),
        padding: EdgeInsets.all(30.w),
        width: 900.w,
        height: 1090.h,
        child: Material(
          child: TableCalendarInner(onEvenLoad: onEvenLoad),
        ),
      ),
    );
  }
}

class TableCalendarInner extends StatefulWidget {
  const TableCalendarInner({super.key, this.onEvenLoad});
  final EvenLoadCallback? onEvenLoad;

  @override
  State<TableCalendarInner> createState() => _TableCalendarInnerState();
}

class _TableCalendarInnerState extends State<TableCalendarInner> {
  CalendarFormat _calendarFormat = CalendarFormat.month;
  DateTime _focusedDay = DateTime.now();
  DateTime? _selectedDay;

  _TableCalendarInnerState();

  @override
  Widget build(BuildContext context) {
    return TableCalendar<bool>(
      locale: Get.locale?.languageCode,
      headerStyle: const HeaderStyle(
        formatButtonVisible: false,
        titleCentered: true,
      ),
      calendarStyle: CalendarStyle(
        outsideDaysVisible: false,
        markerSize: 20.w,
        markerDecoration: const BoxDecoration(color: Colors.green, shape: BoxShape.circle),
        markerMargin: EdgeInsets.only(top: 20.h),
        // todayTextStyle: const TextStyle(color: Colors.black54),
        todayDecoration: BoxDecoration(
          color: Colors.red,
          shape: BoxShape.circle,
          border: Border.all(width: 1, color: Colors.red),
        ),
        selectedDecoration: const BoxDecoration(color: Colors.red, shape: BoxShape.circle),
      ),
      pageJumpingEnabled: true,
      shouldFillViewport: true,
      rowHeight: 10,
      firstDay: DateUtils.addMonthsToMonthDate(DateTime.now(), -12),
      lastDay: DateTime.now(),
      focusedDay: _focusedDay,
      eventLoader: (day) => widget.onEvenLoad!(day),
      selectedDayPredicate: (day) {
        // Use `selectedDayPredicate` to determine which day is currently selected.
        // If this returns true, then `day` will be marked as selected.

        // Using `isSameDay` is recommended to disregard
        // the time-part of compared DateTime objects.
        return isSameDay(_selectedDay, day);
      },
      onDaySelected: (selectedDay, focusedDay) {
        if (!isSameDay(_selectedDay, selectedDay)) {
          // Call `setState()` when updating the selected day
          setState(() {
            _selectedDay = selectedDay;
            _focusedDay = focusedDay;
            Future.delayed(
              const Duration(milliseconds: 200),
              () => Navigator.pop(context, _focusedDay),
            );
          });
        }
      },
      onFormatChanged: (format) {
        if (_calendarFormat != format) {
          // Call `setState()` when updating calendar format
          setState(() {
            _calendarFormat = format;
          });
        }
      },
      onPageChanged: (focusedDay) {
        // No need to call `setState()` here
        _focusedDay = focusedDay;
      },
    );
  }
}
